package com.cryptoneedle.garden.service.garden.source.enhance;


import com.cryptoneedle.garden.entity.source.enhance.SourceDimension;
import com.cryptoneedle.garden.entity.source.metadata.SourceCatalog;
import com.cryptoneedle.garden.entity.source.metadata.SourceColumn;
import com.cryptoneedle.garden.entity.source.metadata.SourceDatabase;
import com.cryptoneedle.garden.entity.source.metadata.SourceTable;
import com.cryptoneedle.garden.enums.SourceDatabaseType;
import com.cryptoneedle.garden.service.crud.garden.source.*;
import com.cryptoneedle.garden.service.util.BucketUtil;
import com.cryptoneedle.garden.service.util.ConfigUtil;
import com.cryptoneedle.garden.service.util.OracleUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>description: 源数据源-转换服务 </p>
 *
 * @author CryptoNeedle
 * @date 2025-06-23
 */
@Slf4j
@Service
public class TransService {

    @Resource
    private TransService transService;

    @Autowired
    private FoundSourceService select;
    @Autowired
    private AddSourceService add;
    @Autowired
    private PatchSourceService patch;
    @Autowired
    private SaveSourceService save;
    @Autowired
    private DeleteSourceService delete;

    @Resource
    private ConfigUtil configUtil;

    @Transactional
    public void calcuteDataDistribute(SourceCatalog catalog, SourceDatabase database) {
        List<SourceTable> tables = select.tables();
        for (SourceTable table : tables) {
            transService.calcuteDataDistribute(catalog, database, table);
        }
    }

    @Transactional
    public void calcuteDataDistribute(SourceCatalog catalog, SourceDatabase database, SourceTable table) {
        if (StringUtils.isBlank(table.getActiveDimensionName())) {
            return;
        }

        List<SourceDimension> dimensions = select.dimensionsEnabled(table);
        if (dimensions.isEmpty()) {
            return;
        }

        int bucketNum = BucketUtil.estimateBucket(table.getEstimateMegaBytes().doubleValue(), 0.4);

        table.setTransBucketCount(bucketNum);
        save.save(table);
    }

    @Transactional
    public void transformDatabase(SourceCatalog catalog, SourceDatabase database) {
        List<SourceTable> tables = select.tables(database);
        for (SourceTable table : tables) {
            transService.transformTable(catalog, database, table);
        }
    }

    @Transactional
    public void transformTable(SourceCatalog catalog, SourceDatabase database, SourceTable table) {
        if (!table.getTransTableNameManual() && StringUtils.isNotEmpty(table.getSystemCode())) {
            table.setTransTableName(configUtil.ods() + "_" + table.getSystemCode() + "_" + StringUtils.lowerCase(table.getTableName()) + "_h");
        }
        if (!table.getTransCommentManual()) {
            table.setTransComment(StringUtils.trimToEmpty(table.getComment()));
        }
        if (table.getTransComment() == null) {
            table.setTransComment("");
        }
        save.save(table);

        List<SourceColumn> columns = select.columns(table);
        for (SourceColumn column : columns) {
            // 转换列
            transService.transformColumn(catalog, database, table, column);
        }
        save.columns(columns);
    }

    private void transformColumn(SourceCatalog catalog, SourceDatabase database, SourceTable table, SourceColumn column) {
        switch (catalog.getDatabaseType()) {
            case SourceDatabaseType.ORACLE:
                OracleUtil.transformDataType(column);
            case SourceDatabaseType.MYSQL: // todo
            case SourceDatabaseType.POSTGRESQL: // todo
            case SourceDatabaseType.SQLSERVER: // todo
        }

        column.setTransTableName(table.getTransTableName());
        if (column.getTransColumnNameManual()) {
            String columnName = column.getColumnName();
            // 去除开头_
            String transformColumnName = Strings.CI.removeStart(columnName, "_");
            // 去除结尾_
            transformColumnName = Strings.CI.removeEnd(transformColumnName, "_");
            // 去除连续_
            transformColumnName = transformColumnName.replaceAll("_{2,}", "_");
            // 小写
            transformColumnName = StringUtils.lowerCase(transformColumnName);
            column.setTransColumnName(transformColumnName);
        }
        if (column.getTransCommentManual()) {
            column.setTransComment(column.getComment());
        }
        if (column.getTransComment() == null) {
            column.setTransComment("");
        }
        // todo transEstimateBytes
    }


}